package com.viroyal.monitorlib.entity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.viroyal.monitorlib.network.Api;
import com.viroyal.monitorlib.utils.AppUtils;
import com.viroyal.monitorlib.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/11/17.
 */

public class HBbean {

    public String action;
    public String dev_sn;
    @SerializedName("app_list")
    public List<AppBean> mAppBeanList;

    @SerializedName("top_app_list")
    public List<TopActivityInfo> mTopActivityInfoList;
    public long id = 0;

    public static HBbean build() {
        return new HBbean();
    }

    public HBbean hB() {
        action = "heart-beat";
        return this;
    }

    public HBbean pkgInfo() {
        action = "pkg_info";
        return this;
    }

    public HBbean TopError() {
        return this;
    }

    public HBbean id(long id) {
        this.id = id;
        return this;
    }

    public HBbean dev(Context context) {
        dev_sn = Api.get().getDeviceId(context);
        mAppBeanList = new ArrayList<>();
        List<PackageInfo> list = AppUtils.getInstalledApps(context);
        for (PackageInfo packageInfo : list) {
            if (packageInfo.packageName.contains("viroyal")) {
                mAppBeanList.add(new AppBean(packageInfo.packageName,
                        packageInfo.versionCode + "", packageInfo.versionName));
            }
        }
        return this;
    }

    @Override
    public String toString() {
        return new Gson().toJson(this);
    }


    public HBbean top(Context context) {
        action = "top";
        mTopActivityInfoList = new ArrayList<>();
        if (!TextUtils.isEmpty(SPUtils.getInstance(context.getApplicationContext()).get("pkg"))) {
            String packageName = SPUtils.getInstance(context.getApplicationContext()).get("pkg");
            String className = SPUtils.getInstance(context.getApplicationContext()).get("class");
            String name = SPUtils.getInstance(context.getApplicationContext()).get("pkg");
            ResolveInfo r = getAppInfo(context, name);
            if (r != null) {
                name = r.loadLabel(context.getPackageManager()).toString();
            }
            mTopActivityInfoList.add(new TopActivityInfo(
                    packageName,
                    className,
                    name));
        }
        return this;
    }

    public static ResolveInfo getAppInfo(Context context, String packagename) {
        Intent intent = new Intent(Intent.ACTION_MAIN, null);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> mAppInfo = context.getPackageManager().queryIntentActivities(intent, 0);
        for (ResolveInfo resolveInfo : mAppInfo) {
            if (resolveInfo.activityInfo.packageName.equals(packagename)) {
                return resolveInfo;
            }
        }
        return null;
    }

    public static class TopActivityInfo {
        @SerializedName("package_name")
        public String packageName = "";
        @SerializedName("top_activity_name")
        public String topActivityName = "";
        @SerializedName("app_name")
        public String appName = "";

        public TopActivityInfo() {
        }

        public TopActivityInfo(String packageName, String topActivityName, String appName) {
            this.packageName = packageName;
            this.topActivityName = topActivityName;
            this.appName = appName;
        }

    }

}
